//
//  ISStopProtectViewController.m
//  iPhoneSafety
//
//  Created by dlon 2018/12/31.
//  Copyright © 2018年 com.li.ding. All rights reserved.
//

#import "ISStopProtectViewController.h"
#import "ISProtectLogoCollectionViewCell.h"
#import "ISProtectItemCollectionViewCell.h"
#import "NWLazyProperty.h"
#import "ISGradientLayerView.h"
#import "UIColor+NWHex.h"
#import <Masonry/Masonry.h>
#import <BlocksKit/BlocksKit.h>
#import <libextobjc/EXTScope.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import "ISSafetyAlarmMamanger.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface ISStopProtectViewController()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *list;
@property (nonatomic, strong) ISProtectLogoCollectionViewCell *logView;

@property (nonatomic, strong) UIButton *actionButton;
@property (nonatomic, strong) ISGradientLayerView *bgView;
@end

@implementation ISStopProtectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_interactivePopDisabled = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] init];
    
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.logView];
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.actionButton];
    
    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(202, 58.f));
        make.bottom.equalTo(self.view).offset(-51.f);
        make.centerX.equalTo(self.view);
    }];
    
    [self.logView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.height.mas_equalTo(220.f);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.logView.mas_bottom);
        make.bottom.equalTo(self.actionButton.mas_top);
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self loadData];
    
    @weakify(self)
    [self.actionButton bk_addEventHandler:^(id sender) {
        @strongify(self)
        [[ISSafetyAlarmMamanger sharedInstance] stop];
        [self.navigationController popViewControllerAnimated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:kISSafetyAlarmStateDidChangeNotification object:nil];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)loadData {
    
    [self.list removeAllObjects];
    
    ISSafetyAlarmStrategy strategy = [ISSafetyAlarmMamanger sharedInstance].strategy;
    
    if (strategy & ISSafetyAlarmStrategyAirplan) {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISProtectItemCollectionViewCell";
        dict[@"height"] = @(60.f);
        dict[@"title"] = @"飞行防盗已激活";
        dict[@"isOn"] = @([ISSafetyAlarmMamanger sharedInstance].alarmAirplanState == ISSafetyAlarmAirplanStateReady);
        [self.list addObject:dict];
    }
    
    if (strategy & ISSafetyAlarmStrategyPocket) {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISProtectItemCollectionViewCell";
        dict[@"height"] = @(60.f);
        if ([ISSafetyAlarmMamanger sharedInstance].alarmPocketState == ISSafetyAlarmPocketStateReady) {
            dict[@"title"] = @"口袋防盗已激活";
            dict[@"isOn"] = @(YES);
        } else {
            dict[@"title"] = @"请放置口袋激活";
            dict[@"isOn"] = @(NO);
        }
        [self.list addObject:dict];
    }
    
    if (strategy & ISSafetyAlarmStrategyDesk) {
        
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISProtectItemCollectionViewCell";
        dict[@"height"] = @(60.f);
        if ([ISSafetyAlarmMamanger sharedInstance].alarmDeskState == ISSafetyAlarmDeskStateReady) {
            dict[@"title"] = @"桌面防盗已激活";
            dict[@"isOn"] = @(YES);
        } else {
            dict[@"title"] = @"请放置桌面激活";
            dict[@"isOn"] = @(NO);
        }
        [self.list addObject:dict];
    }
    
    if (strategy & ISSafetyAlarmStrategyCharging) {
        
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISProtectItemCollectionViewCell";
        dict[@"height"] = @(60.f);
        if ([ISSafetyAlarmMamanger sharedInstance].alarmChargingState == ISSafetyAlarmChargingStateReady) {
            dict[@"title"] = @"电源防盗已激活";
            dict[@"isOn"] = @(YES);
        } else {
            dict[@"title"] = @"请插上电源激活";
            dict[@"isOn"] = @(NO);
        }
        [self.list addObject:dict];
    }
    
    if (strategy & ISSafetyAlarmStrategyHeadset) {
        NSMutableDictionary *dict = @{}.mutableCopy;
        dict[@"type"] = @"ISProtectItemCollectionViewCell";
        dict[@"height"] = @(60.f);
        if ([ISSafetyAlarmMamanger sharedInstance].alarmHeadsetState == ISSafetyAlarmHeadsetStateReady) {
            dict[@"title"] = @"耳机防盗已激活";
            dict[@"isOn"] = @(YES);
        } else {
            dict[@"title"] = @"请插上耳机激活";
            dict[@"isOn"] = @(NO);
        }
        [self.list addObject:dict];
    }
    
    [self.collectionView reloadData];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (@available(iOS 11.0, *)) {
        [self.logView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(self.view.safeAreaInsets.top);
        }];
    } else {
        [self.logView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(64.f);
        }];
    }
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *viewModel = self.list[indexPath.row];
    NSString *type = viewModel[@"type"];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:type forIndexPath:indexPath];
    
    if ([type isEqualToString:@"ISProtectItemCollectionViewCell"]) {
        ISProtectItemCollectionViewCell *itemCell = (ISProtectItemCollectionViewCell *)cell;
        itemCell.tipLabel.text = viewModel[@"title"];
        itemCell.isOn = [viewModel[@"isOn"] boolValue];
    }
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.list.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *viewModel = self.list[indexPath.row];
    
    return CGSizeMake(CGRectGetWidth(collectionView.frame), [viewModel[@"height"] doubleValue]);
}


#pragma mark - set & get
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsZero;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [_collectionView registerClass:[ISProtectLogoCollectionViewCell class] forCellWithReuseIdentifier:@"ISProtectLogoCollectionViewCell"];
        [_collectionView registerClass:[ISProtectItemCollectionViewCell class] forCellWithReuseIdentifier:@"ISProtectItemCollectionViewCell"];
        
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

NWLazyPropertyWithInit(NSMutableArray, list, {
    
})

NWLazyPropertyWithInit(ISProtectLogoCollectionViewCell, logView, {
    
})

- (ISGradientLayerView *)bgView {
    if (!_bgView) {
        _bgView = [ISGradientLayerView new];
        CAGradientLayer *layer = (CAGradientLayer *)_bgView.layer;
        layer.startPoint = CGPointMake(0, 0);
        layer.endPoint = CGPointMake(0, 1);
        layer.colors = @[(id)[UIColor nw_colorWithHex:0x0E6AF3].CGColor,(id)[UIColor nw_colorWithHex:0x05B3F4].CGColor];
    }
    return _bgView;
}

NWLazyPropertyWithInit(UIButton, actionButton, {
    _actionButton.backgroundColor = [UIColor clearColor];
    _actionButton.layer.cornerRadius = 29;
    _actionButton.clipsToBounds = YES;
    _actionButton.layer.borderWidth = 2.f;
    _actionButton.layer.borderColor = [UIColor nw_colorWithHex:0xffffff alpha:0.2].CGColor;
    [_actionButton setTitle:@"停止保护" forState:UIControlStateNormal];
    _actionButton.titleLabel.font = [UIFont systemFontOfSize:20.f];
    [_actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
})
@end
